package com.icejoywoo.rss;

import org.apache.cordova.DroidGap;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.Window;
import android.view.WindowManager;

/**
 * main activity
 * @author icejoywoo
 *
 */
public class RssReaderActivity extends DroidGap {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setFullscreen();
		// starting splash
		super.setStringProperty("loadingDialog", "正在启动...");
		super.loadUrl("file:///android_asset/www/simple.html", 1000);
	}
	
	// 全屏（无标题栏和状态栏）
	public void setFullscreen() {
		getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);// 清除FLAG
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
	}
	
    @Override  
    public boolean onKeyDown(int keyCode, KeyEvent event) {  
        // 按下键盘上返回按钮, 并且appView无法返回的时候, 就退出
        if (keyCode == KeyEvent.KEYCODE_BACK && !this.appView.canGoBack()) {
        	quitWithDialog();
            return true;
        } else {
            return super.onKeyDown(keyCode, event);  
        }  
    }

    private static int MAIN_GROUP = 0;
    private static int MAIN_QUIT = 0;
    
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		SubMenu mainMenu = menu.addSubMenu(MAIN_GROUP, MAIN_QUIT, 0, "退出");
		mainMenu.setIcon(R.drawable.quit);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		if (id == MAIN_QUIT) {
			quitWithDialog();
		}
		return super.onOptionsItemSelected(item);
	}
    
	/**
	 * 有提示框的退出, 选择确认退出, 选择取消无任何操作
	 */
    private void quitWithDialog() {
    	new AlertDialog.Builder(this)  
        .setTitle("RSS阅读器")  
        .setMessage("确定退出?")  
        .setNegativeButton("取消",  
                new DialogInterface.OnClickListener() {  
                public void onClick(DialogInterface dialog, int which) {
                		
                   }  
                })  
        .setPositiveButton("确认",  
                new DialogInterface.OnClickListener() {  
                    public void onClick(DialogInterface dialog, int whichButton) {  
                        finish();  
                    }  
                }).show();
    }

}